package com.university.fragment;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

import android.app.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myuniversity.R;
import com.university.utils.SqliteDatabaseManager;

public class RegionFragment extends Fragment {
	private View view;
	private RadioGroup radioGroupFirst,radioGroupSecond;
	private RadioButton radioHuadong,radioHuanan,radioDongbei,radioHuazhong,radioHuabei,radioXinan,radioXibei,radioGangao;
	private ListView listView;
	private SearchView searchView;
	private int id;
	private boolean flag=true;
	private SQLiteDatabase mDatabase;
	private Cursor cursor;
	private LayoutInflater mInflater;
	private String region;
	private BaseAdapter adapter;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater=inflater;
		mDatabase=SqliteDatabaseManager.getInstance(getActivity()).getDatabase();
		view=inflater.inflate(R.layout.fragment_region, container, false);
		cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","地区","录取数"}, null, null, "院校代码,地区", null, null);
		init();
		setOnClick();
		adapter=new MyAdapter();
		listView.setAdapter(adapter);
		
		//设置searchview的提交按钮显示，默认不显示
				searchView.setSubmitButtonEnabled(true);
				//反射来获得searchView的自动完成框变量
		        Class<?> argClass = searchView.getClass();     
			    //--指定某个私有属性,mSearchPlate是搜索框父布局的名字        
			    Field ownField;
				try {
					ownField = argClass.getDeclaredField("mSubmitButton");
					ownField.setAccessible(true);     
					ImageView imageSubmit = (ImageView) ownField.get(searchView);
					imageSubmit.setImageResource(android.R.drawable.ic_menu_search);
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				}
		return view;
	}
	private void init() {
		radioGroupFirst=(RadioGroup) view.findViewById(R.id.radio_group_first);
		radioGroupSecond=(RadioGroup) view.findViewById(R.id.radio_group_second);
		listView=(ListView) view.findViewById(R.id.listview_fragment_region);
		searchView=(SearchView) view.findViewById(R.id.searchview_fragment_region);
		radioDongbei=(RadioButton) view.findViewById(R.id.radio_region_dongbei);
		radioGangao=(RadioButton) view.findViewById(R.id.radio_region_gangao);
		radioHuabei=(RadioButton) view.findViewById(R.id.radio_region_huabei);
		radioHuadong=(RadioButton) view.findViewById(R.id.radio_region_huadong);
		radioHuanan=(RadioButton) view.findViewById(R.id.radio_region_huanan);
		radioHuazhong=(RadioButton) view.findViewById(R.id.radio_region_huazhong);
		radioXibei=(RadioButton) view.findViewById(R.id.radio_region_xibei);
		radioXinan=(RadioButton) view.findViewById(R.id.radio_region_xinan);
	}
	private void setOnClick() {
		radioDongbei.setOnClickListener(new onRadioClick());
		radioGangao.setOnClickListener(new onRadioClick());
		radioHuabei.setOnClickListener(new onRadioClick());
		radioHuadong.setOnClickListener(new onRadioClick());
		radioHuanan.setOnClickListener(new onRadioClick());
		radioHuazhong.setOnClickListener(new onRadioClick());
		radioXibei.setOnClickListener(new onRadioClick());
		radioXinan.setOnClickListener(new onRadioClick());
		
		radioGroupFirst.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
					id=checkedId;
					//如果另一个组为空则不做操作
					if (flag) {
						flag=false;
						if (radioGroupSecond.getCheckedRadioButtonId()==-1) {
							radioGroupSecond.check(checkedId);
						}else {
							radioGroupSecond.clearCheck();
						}
						flag=true;
					}
			}
		});
		radioGroupSecond.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
					id=checkedId;
					if (flag) {
						flag=false;
						if (radioGroupFirst.getCheckedRadioButtonId()==-1) {
							radioGroupFirst.check(id);
						}else {
							radioGroupFirst.clearCheck();
						}
						flag=true;
					}
				
			}
		});
		
		searchView.setOnQueryTextListener(new OnQueryTextListener() {
			@Override
			public boolean onQueryTextSubmit(String query) {
				cursor.close();
				cursor=mDatabase.query("院校表",new String[]{"院校代码","院校名称","地区","录取数"}, "地区 like ?", new String[]{"%"+query+"%"}, "院校代码,地区", null, null);
				adapter.notifyDataSetChanged();
				return false;
			}
			
			@Override
			public boolean onQueryTextChange(String newText) {
				if (newText.length()==0) {
					cursor.close();
					cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","地区","录取数"}, null, null, "院校代码,地区", null, null);
					adapter.notifyDataSetChanged();
				}
				return false;
			}
		});
	}
	
	class onRadioClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.radio_region_huadong:
				region="华东";
				break;
			case R.id.radio_region_huabei:
				region="华北";
				
				break;
			case R.id.radio_region_huanan:
				region="华南";
				
				break;
			case R.id.radio_region_huazhong:
				region="华中";
				
				break;
			case R.id.radio_region_dongbei:
				region="东北";
				
				break;
			case R.id.radio_region_xibei:
				region="西北";
				
				break;
			case R.id.radio_region_xinan:
				region="西南";
				
				break;
			case R.id.radio_region_gangao:
				region="港澳台";
				break;

			default:
				break;
			}
		}
	}
	private class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return cursor.getCount();
		}
		@Override
		public Object getItem(int position) {
			return cursor.moveToPosition(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			cursor.moveToPosition(position);
			ViewHolder viewHolder;
			
			if (convertView==null) {
				viewHolder=new ViewHolder();
				convertView=mInflater.inflate(R.layout.layout_region_listview_item, parent, false);
				viewHolder.uId=(TextView) convertView.findViewById(R.id.txt_region_listview_item_uid);
				viewHolder.uName=(TextView) convertView.findViewById(R.id.txt_region_listview_item_uname);
				viewHolder.uRegion=(TextView) convertView.findViewById(R.id.txt_region_listview_item_region);
				viewHolder.number=(TextView) convertView.findViewById(R.id.txt_region_listview_item_number);
				convertView.setTag(viewHolder);
			}else {
				viewHolder=(ViewHolder) convertView.getTag();
			}
			viewHolder.uId.setText(cursor.getString(0));
			viewHolder.uName.setText(cursor.getString(1));
			viewHolder.uRegion.setText(cursor.getString(2));
			viewHolder.number.setText(cursor.getString(3));
			return convertView;
		}
		
	}
	static class ViewHolder{
		TextView uId,uName,uRegion,number;
	}
}
